﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
using AventStack.ExtentReports.Utils;
using Ro.AssistFunction.BackNodeInfo;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface.UserType;

namespace Ro.Interpreter.InterAssistFunc
{
    /// <summary>
    /// 泛型类
    /// 协助处理Tci信息
    /// 提取有效信息
    /// web
    /// 将tci信息直接提取到一个list列表内
    /// 返回的是ControlUseType
    /// </summary>
    public class ExtractTciInfo<T>

    {
        ///返回值
        public T BackType;

        /// <summary>
        /// 解析tci文件
        /// </summary>
        /// <param name="config">web对应TCI类，单个</param>
        public ExtractTciInfo(ConfigurationFile config)
        {
            //给定纯的 .tci文件  
            string compath;
            if (!config.Path.Contains("${"))
            {
                //Pro版本
                if (RoPubArgments.ProTips)
                {
                    compath = RoPubArgments.GuiTestArgsInfo.TciFolderPath + "/" + config.Path;
                }
                else
                {
                    compath = RoPubArgments.RoTestArgsInfo.TciFolderPath + "/" + config.Path;
                }
            }
            else
            {
                //返回已将参数转为值的tci路径 todo 5-4更新完成，支持多形式参数调用
                ArgsIntoValue argsIntoValue = new ArgsIntoValue(config.Path);
                compath = argsIntoValue.BackValue; //返回已将参数转为值的tci路径 todo 5-4更新完成，支持多形式参数调用
            }

            //解析Tci文件, 返回RoWeb.OneTciInfos，转换类型
            BackType = (T) Convert.ChangeType(AnalyzeTciFileMethod(compath, config.Id), typeof(T));
        }


        /// <summary>
        /// 私有函数
        /// 解析Web的Tci文件
        /// </summary>
        /// <param name="tcipath">tci路径</param>
        /// <param name="tciid">tci的Id</param>
        /// <returns>返回自定义RoWeb.ControlUseType类</returns>
        private RoWeb.ControlUseType AnalyzeTciFileMethod(string tcipath, string tciid)
        {
            //定义临时的返回值病初始化，写入ID值
            RoWeb.ControlUseType controlUseType = new RoWeb.ControlUseType
            {
                TciId = tciid,
                OneTciInfos = new List<RoWeb.OneTciInfo>()
            };

            if (!tcipath.Contains("ComTci"))
            {
                //创建 xmldoc 完成tci解析
                //todo 更换为LINQ的方式解析XML文件，可以更快速的获取对应信息,可以直接跳过注释 Update 2017-6-8 更新解析XSD文件
                XElement root = XElement.Load(tcipath);
                //添加namespace
                XNamespace xs = "http://www.w3.org/2001/XMLSchema";
                //查找所有simpletype
                IEnumerable<XElement> eles = root.Elements(xs + "simpleType");


                //找到与tciid相等的项目并将restriction给定出
                XElement useele = (from ele in eles where (ele.Attributes("name").Single().Value == tciid) select ele).Single().Elements(xs + "restriction").Single();

                foreach (XElement xElement in useele.Elements(xs + "enumeration"))
                {
                    //定义一个onetciinfo,传入ControlUseType中
                    RoWeb.OneTciInfo oneTciInfo = new RoWeb.OneTciInfo();
                    oneTciInfo.ControlName = (from att in xElement.Attributes() where att.Name == "value" select att).Single().Value;

                    //appinfo信息
                    XElement annele = (from element in xElement.Elements() where element.Name == xs + "annotation" select element).Single();

                    XElement firstinfo = annele.Descendants(xs + "appinfo").First();
                    XElement lastinfo = annele.Descendants(xs + "appinfo").Last();

                    oneTciInfo.Locator = (from att in firstinfo.Attributes() where att.Name == "source" select att).Single().Value;
                    oneTciInfo.Value = firstinfo.Value;

                    //RoPubArgments.RoLog.Debug("当前解析的LastInfo" + lastinfo.Value);

                    if (lastinfo.Value.IsNullOrEmpty())
                    {
                        oneTciInfo.Index = -1;
                    }
                    else
                    {
                        oneTciInfo.Index = Convert.ToInt32(lastinfo.Value);
                    }

                    //添加一个web tci信息
                    RoPubArgments.RoLog.Debug("{" + oneTciInfo.ControlName + "," + oneTciInfo.Locator + "," + oneTciInfo.Value + "," +oneTciInfo.Index+"}");
                    controlUseType.OneTciInfos.Add(oneTciInfo);
                }
            }
            return controlUseType;
        }
    }
}
    

